//
//  EBMBaseTableView.m
//  eBondMall
//
//  Created by apple on 2020/3/31.
//  Copyright © 2020 zhaoshijie. All rights reserved.
//

#import "EBMBaseTableView.h"
#import "SJNetStatusTool.h"
#import "EBMBaseTableViewCell.h"

@implementation EBMBaseTableView


- (void)awakeFromNib
{
    [super awakeFromNib];
    
    // 创建视图
    [self baseInitView];
    
}


- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if(self)
    {
        // 创建视图
        [self baseInitView];
        
    }
    return self;
}



#pragma mark -- --------------------创建视图--------------------
// 创建视图
- (void)baseInitView
{
    
    
    
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.backgroundColor = RGBCOLOR16_F1F2F3;
    
    self.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    self.emptyDataSetSource = self;
    self.emptyDataSetDelegate = self;
    
    [self registerClassWithCellClass:[EBMBaseTableViewCell class]];
    
    
    
}



#pragma mark --------------方法--------------


/// 添加上下拉刷新加载
/// @param headrRefreshingBlock 下拉刷新
/// @param footerRefreshingBlock 上拉加载
- (void)loadTableMjHeaderWithHeadrRefreshingBlock:(MJRefreshComponentAction)headrRefreshingBlock
                            footerRefreshingBlock:(MJRefreshComponentAction)footerRefreshingBlock
{
    if(headrRefreshingBlock)
    {
        //  添加上下拉刷新加载
        self.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:headrRefreshingBlock];
        
        MJRefreshNormalHeader *mj_header = (MJRefreshNormalHeader *)self.mj_header;
        
        mj_header.lastUpdatedTimeLabel.hidden = YES;
        
        mj_header.stateLabel.textColor = RGBCOLOR16_888888;
        mj_header.stateLabel.font = Font_12;
        [mj_header setTitle:@"松手即可刷新" forState:MJRefreshStatePulling];
        

        
        [self.mj_header endRefreshingWithCompletionBlock:^{
            
            self.isLoadingStatus = NO;

            //  tableView结束刷新
            [self baseEndRefresh];
            
            
        }];
        
    }
    
    if(footerRefreshingBlock)
    {
        //  添加上下拉刷新加载
        self.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:footerRefreshingBlock];
        
        MJRefreshBackStateFooter *mj_footer = (MJRefreshBackStateFooter *)self.mj_footer;
        
        mj_footer.stateLabel.textColor = RGBCOLOR16_888888;
        mj_footer.stateLabel.font = Font_12;
        [mj_footer setTitle:@"松手即可刷新" forState:MJRefreshStatePulling];
        
        
        [self.mj_footer endRefreshingWithCompletionBlock:^{
            
            self.isLoadingStatus = NO;
            
            //  tableView结束刷新
            [self baseEndRefresh];
            
            
        }];
        
    }
    
}


/// 下拉刷新
- (void)beginRefreshing
{
    
//    self.contentOffset = CGPointMake(0, 0 );

    self.isLoadingStatus = YES;

    //  在主线程刷新背景视图
    [self reloadEmptyDataSetInDispatchMain];

    //  刷新
    [self.mj_header beginRefreshing];


    //  延迟调用。不然有bug
//    [self performSelector:@selector(delayMethod) withObject:nil afterDelay:0.18];
    
    [self delayMethod];
    
}



/// 刷新
- (void)baseReloadData
{
    
    self.isLoadingStatus = NO;
    
    [super reloadData];
    
    //  在主线程刷新背景视图
    [self reloadEmptyDataSetInDispatchMain];
    
    //  tableView结束刷新
    [self baseEndRefresh];
    
}



/// tableView结束刷新
- (void)baseEndRefresh
{
    dispatch_async(dispatch_get_main_queue(), ^{

        
        [self.mj_header endRefreshing];
        
        [self.mj_footer endRefreshing];
        
    });
    
    
}




//  在主线程刷新背景视图
- (void)reloadEmptyDataSetInDispatchInRqeust
{
    
    if(!self.isLoadingStatus)
    {
        self.isLoadingStatus = YES;

        //  在主线程刷新背景视图
        [self reloadEmptyDataSetInDispatchMain];
    }
    
    
}

//  在主线程刷新背景视图
- (void)reloadEmptyDataSetInDispatchMain
{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        

        //  刷新数据
        [self reloadEmptyDataSet];
        
    });
    
    
}


/// 注册cell nib
/// @param className cell名称
- (void)registerNibWithCellClassName:(NSString *)className
{
    [self registerNib:[UINib nibWithNibName:className bundle:nil] forCellReuseIdentifier:className];
}


/// 注册cell class
/// @param class cell的类
- (void)registerClassWithCellClass:(Class)class
{
    [self registerClass:class forCellReuseIdentifier:NSStringFromClass(class)];
}


/// 刷新section
/// @param section 部分
- (void)reloadSectionsWithSection:(NSInteger)section
{
    
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:section];
    [self reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
    
}



//  延迟调用
- (void)delayMethod
{

    dispatch_async(dispatch_get_main_queue(), ^{
        

        //  下拉刷新
        [self.mj_header beginRefreshing];
        
    });
    
}



#pragma mark ----------DZNEmptyDataSetSource 代理方法----------
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    UIImage *image = [UIImage imageNamed:@"Bond_Main_Null"];

    NSInteger netWorkStatus = [SJNetStatusTool gainNetStatusToolInType];
    
    if(netWorkStatus == 0)
    {
        image = [UIImage imageNamed:@"Bond_Main_NoNet"];
    }

    //  结束刷新
    if(self.isLoadingStatus)
    {
        return nil;
    }


    return image;

}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {


    NSString *title = @"暂无数据";

    NSInteger netWorkStatus = [SJNetStatusTool gainNetStatusToolInType];
    if(netWorkStatus == 0)
    {
//        title = @"网络异常，请检查网络再点击刷新";
        title = @"加载失败";

    }

    if(self.userLoginStatus)
    {
        if(!KUserSingeIsLogin)
        {
            title = @"请先登录";
        }
    }


    //  结束刷新
    if(self.isLoadingStatus)
    {
        title = @"刷新中...";
    }


    NSDictionary *attributes = @{
                                 NSFontAttributeName:Font_14,
                                 NSForegroundColorAttributeName:RGBCOLOR16_888888
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];

}


- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {

    NSInteger netWorkStatus = [SJNetStatusTool gainNetStatusToolInType];
    if(netWorkStatus != 0)
    {
        return nil;
    }
    
    if(!self.mj_header)
    {
        return nil;
    }

    //  结束刷新
    if(self.isLoadingStatus)
    {
        return nil;
    }
    

    return [UIImage imageNamed:@"Bond_Main_NoData_Refresh"];
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {

    return [UIColor whiteColor];

}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {

    if(self.mj_header)
    {

        //  收起键盘
        [self.viewController.view endEditing:YES];

        /// 下拉刷新
        [self beginRefreshing];

    }
}


- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
    
    if(!self.mj_header)
    {
        return;
    }
    

    if(self.userLoginStatus)
    {

        KUserSingeNoLoginGoLogin(self.viewController)

    }

    if(self.mj_header && !self.isLoadingStatus)
    {


        //  收起键盘
        [self.viewController.view endEditing:YES];

        /// 下拉刷新
        [self beginRefreshing];

    }

}


- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{

    return -self.frame.size.height*0.15;

}


//  背景将要显示
//  DZNEmptyDataSetView是根据正在刷新的过程中给其定布局的。然而我们需要阻止它或延后它
- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView {

    scrollView.contentOffset = CGPointMake(0, 0 );
    
//    scrollView.contentOffset = CGPointZero;

}


@end
